package com.xkdx.guangguang;

import java.util.Timer;

import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.xkdx.caipiao.HomePageActivity;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.brand.BrandInfoFragment;
import com.xkdx.guangguang.fragment.brand.branch.BranchFragment;
import com.xkdx.guangguang.fragment.shopinfo.ShopInfoFragment;
import com.xkdx.guangguang.module.cache.XkCacheManager;
import com.xkdx.guangguang.module.statics.IConstants;

public class WelcomeActivity extends Activity {

	private Timer timer;
	private ImageView im;
	private Bitmap bitmap;

	// private boolean isGuide;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		IConstants.DisplayWidth = dm.widthPixels;
		super.onCreate(savedInstanceState);

		setContentView(R.layout.welcome);
		im = (ImageView) findViewById(R.id.surfaceView);
		bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.welcome);
		im.setBackgroundDrawable(new BitmapDrawable(bitmap));

		// /**
		// * 判断是否需要引导页
		// */
		//
		// SharePrefenceUtil sp = new SharePrefenceUtil(getApplicationContext(),
		// IConstants.SP_GUIDE);
		// // 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
		// isGuide = sp.getIsGuide();

		ClearAllCacheTask clearAllCacheTask = new ClearAllCacheTask();
		clearAllCacheTask.execute();

	}

	@Override
	protected void onStart() {
		super.onStart();

	}

	LayoutParams lp;

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if (bitmap != null) {
			bitmap.recycle();
		}
		super.onStop();
	}

	class ClearAllCacheTask extends AsyncTask<String, integer, String> {

		@Override
		protected String doInBackground(String... params) {
			try {
				ShopInfoFragment.attentionShop.clear();
				BrandInfoFragment.attentionBrand.clear();
				BranchFragment.attentionBranch.clear();
				XkCacheManager.clearAllCache();
			} catch (Exception e) {
				e.printStackTrace();
				return "ClearFalse";
			}

			return "clearSuccesee";
		}

		@Override
		protected void onPostExecute(String result) {

			// 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
			// if (!isGuide) {

			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					Intent intent = new Intent(WelcomeActivity.this,
							HomePageActivity.class);
					startActivity(intent);
				}
			}, 1000);
			// } else {
			//
			// // CreateShoutCut(); //创建快捷方式图标
			// new Handler().postDelayed(new Runnable() {
			//
			// @Override
			// public void run() {
			// Intent intent = new Intent(WelcomeActivity.this,
			// GuideActivity.class);
			// startActivity(intent);
			// }
			// }, 1000);
			// }

		}
	}

	// /**
	// * 创建快捷方式
	// */
	//
	// public void CreateShoutCut() {
	// if (!hasShortcut()) {
	// addShortcut();
	// }
	// }
	//
	// /**
	// * 创建操作
	// */
	// private void addShortcut() {
	// Intent shortcut = new Intent(
	// "com.android.launcher.action.INSTALL_SHORTCUT");
	//
	// // 快捷方式的名称
	// shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
	// getString(R.string.app_name));
	// shortcut.putExtra("duplicate", false); // 不允许重复创建
	//
	// Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
	// shortcutIntent.setClassName(this, this.getClass().getName());
	// shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
	//
	// // 快捷方式的图标
	// ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(
	// this, R.drawable.main);
	// shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
	//
	// sendBroadcast(shortcut);
	// }
	//
	// /**
	// * 判断是否已经创建了快捷方式
	// *
	// * @return
	// */
	// private boolean hasShortcut() {
	// boolean isInstallShortcut = false;
	// final ContentResolver cr = this.getContentResolver();
	// final String AUTHORITY = "com.android.launcher.settings";
	// final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
	// + "/favorites?notify=true");
	// Cursor c = cr
	// .query(CONTENT_URI,
	// new String[] { "title", "iconResource" },
	// "title=?",
	// new String[] { this.getString(R.string.app_name).trim() },
	// null);
	// if (c != null && c.getCount() > 0) {
	// isInstallShortcut = true;
	// }
	// return isInstallShortcut;
	// }

}